//
//  CKTabBar.swift
//  CKWeiBo
//
//  Created by 陈科 on 2017/1/4.
//  Copyright © 2017年 CK. All rights reserved.
//

import UIKit

class CKTabBar: UITabBar {

    
    //初始化方法中添加子视图
    override init(frame: CGRect) {
        super.init(frame: frame)
        //添加中间按钮
        self.setupUI()
    }
    //当使用xib,sb创建的时候就会报错
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    //修改子视图的frame
    override func layoutSubviews()
    {
        //一定要先调用父类方法
        super.layoutSubviews()
        
        //获取子控件的尺寸
        let width = UIScreen.main.bounds.width / 5
        let height = bounds.height
        
        var index = 0
        for subView in subviews
        {
            //判断subView是否是UITabBarButton类型的控件
            //UITabBarButton,是私有类,无法获取
            if subView.isKind(of: NSClassFromString("UITabBarButton")!)
            {
                //重新布局
                subView.frame = CGRect(x: CGFloat(index) * width, y: 0, width: width, height: height)
                
                //空出中间的位置
                if index == 1
                {
                    index += 1
                }
                
                index += 1
            }
        }
        //设置中间的按钮
        centerBtn.center = CGPoint(x: self.center.x, y: height / 2.0)
        centerBtn.bounds.size = CGSize(width: width, height: height)
        
    }
    
    func setupUI(){
        self.addSubview(centerBtn)
    }
    
    //MARK:
    //MARK:懒加载
    lazy var centerBtn: UIButton = {
        
        let btn = UIButton(type: .custom)
        
        btn.setImage(#imageLiteral(resourceName: "tabbar_compose_icon_add"), for: .normal)
        btn.setImage(#imageLiteral(resourceName: "tabbar_compose_icon_add_highlighted"), for: .highlighted)
        
        btn.setBackgroundImage(#imageLiteral(resourceName: "tabbar_compose_button"), for: .normal)
        btn.setBackgroundImage(#imageLiteral(resourceName: "tabbar_compose_button_highlighted"), for: .highlighted)
        
        
        
        return btn
    
    }()
    
    
    

}
